package hang.xipujewelWeb.web.account;

import hang.xipujewelWeb.entity.account.User;
import hang.xipujewelWeb.entity.app.ShopAddress;
import hang.xipujewelWeb.service.account.ShopAddressManager;
import hang.xipujewelWeb.service.account.UserManager;
import hang.xipujewelWeb.utils.UserUtil;

import java.util.Map;

import javax.servlet.ServletRequest;

import net.esoar.modules.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/address")
public class AddressController extends BaseController<ShopAddress> {

	@Autowired
	private UserManager userManager;

	@Autowired
	private ShopAddressManager shopAddressManager;

	@RequestMapping("save")
	@ResponseBody
	public String save(Model model, ServletRequest request) {
		Map<String, Object> searchParams = getFilters(request);
		ShopAddress shopAddress = initAddress(searchParams);
		shopAddressManager.save(shopAddress);
		return "修改地址成功";
	}

	private ShopAddress initAddress(Map<String, Object> searchParams) {
		User cUser = userManager.findUserByLoginName(UserUtil.getCurrentUserLoginName());
		ShopAddress shopAddress = cUser.getShopAddress();
		if (shopAddress == null) {
			shopAddress = new ShopAddress();
		}
		shopAddress.setName((String) searchParams.get("name"));
		shopAddress.setCell((String) searchParams.get("cell"));
		shopAddress.setCellphone((String) searchParams.get("cellphone"));
		shopAddress.setAddress((String) searchParams.get("address"));
		shopAddress.setEmail((String) searchParams.get("email"));
		shopAddress.setRemark((String) searchParams.get("remark"));
		shopAddress.setFax((String) searchParams.get("fax"));
		return shopAddress;
	}

	@Override
	public String baseMapping() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModelName() {
		// TODO Auto-generated method stub
		return null;
	}

}
